#include "speed_add_dec.h"
#include "ui_speed_add_dec.h"

speed_add_dec::speed_add_dec(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::speed_add_dec)
{
    ui->setupUi(this);
    initializeBackground();
    connectSignalsAndSlots();
    setDefaultSpeedParam();
    displaySpeedParam();
}

void speed_add_dec::connectSignalsAndSlots()
{
    connect(ui->manualReturnMenu, &QPushButton::clicked, this, &speed_add_dec::pushReturnMenu);
    connect(ui->manualReturnTeachMode, &QPushButton::clicked, this, &speed_add_dec::pushReturnTeachMode);
    connect(ui->manualReturnCaltimer, &QPushButton::clicked, this, &speed_add_dec::pushReturnCalTimer);

    connect(ui->label_axis1_addSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("Y", 1);});
    connect(ui->label_axis1_decSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("Y", -1);});
    connect(ui->label_axis2_addSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("MX", 1);});
    connect(ui->label_axis2_decSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("MX", -1);});
    connect(ui->label_axis3_addSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("MZ", 1);});
    connect(ui->label_axis3_decSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("MZ", -1);});
    connect(ui->label_axis4_addSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("SX", 1);});
    connect(ui->label_axis4_decSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("SX", -1);});
    connect(ui->label_axis5_addSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("SZ", 1);});
    connect(ui->label_axis5_decSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("SZ", -1);});
    connect(ui->label1_addSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("Ratio", 1);});
    connect(ui->label1_decSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("Ratio", -1);});
    connect(ui->label2_addSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("Corrective", 1);});
    connect(ui->label2_decSpeed, &QPushButton::clicked, this, [&](){
        this->changeSpeedParam("Corrective", -1);});

    QList<QPushButton*> buttons = ui->widget->findChildren<QPushButton*>();
    for (QPushButton* button : buttons) {
        connect(button, &QPushButton::clicked, this, [&](){
            this->displaySpeedParam();});
    }
}

void speed_add_dec::initializeBackground()
{
    ui->widget->setAutoFillBackground(true);
    QPalette palette = ui->widget->palette();
    palette.setColor(QPalette::Background, Qt::white);
    ui->widget->setPalette(palette);
}

void speed_add_dec::setDefaultSpeedParam()
{
    if(Pendant_Techn_Param_ptr->AxisY.AxisAcc == 0) {
        Pendant_Techn_Param_ptr->AxisY.AxisAcc = 26;
        Pendant_Techn_Param_ptr->AxisY.AxisAccTime = 0.50;
    }
    if(Pendant_Techn_Param_ptr->AxisMX.AxisAcc == 0) {
        Pendant_Techn_Param_ptr->AxisMX.AxisAcc = 26;
        Pendant_Techn_Param_ptr->AxisMX.AxisAccTime = 0.50;
    }
    if(Pendant_Techn_Param_ptr->AxisMZ.AxisAcc == 0) {
        Pendant_Techn_Param_ptr->AxisMZ.AxisAcc = 26;
        Pendant_Techn_Param_ptr->AxisMZ.AxisAccTime = 0.40;
    }
    if(Pendant_Techn_Param_ptr->AxisSX.AxisAcc == 0) {
        Pendant_Techn_Param_ptr->AxisSX.AxisAcc = 26;
        Pendant_Techn_Param_ptr->AxisSX.AxisAccTime = 0.50;
    }
    if(Pendant_Techn_Param_ptr->AxisSZ.AxisAcc == 0) {
        Pendant_Techn_Param_ptr->AxisSZ.AxisAcc = 26;
        Pendant_Techn_Param_ptr->AxisSZ.AxisAccTime = 0.40;
    }
    if(Pendant_Techn_Param_ptr->PendantServoParam.SpeedRatio == 0)
        Pendant_Techn_Param_ptr->PendantServoParam.SpeedRatio = 100;
    if(Pendant_Techn_Param_ptr->PendantServoParam.CorrectiveAxisAcc == 0)
        Pendant_Techn_Param_ptr->PendantServoParam.CorrectiveAxisAcc = 26;
}

void speed_add_dec::changeSpeedParam(QString axisName, int ifAddOrDec)
{
    if(ifAddOrDec!=1 && ifAddOrDec!=-1)
        return;
    if(axisName == "Y") {
        if((Pendant_Techn_Param_ptr->AxisY.AxisAcc + ifAddOrDec >0) && (Pendant_Techn_Param_ptr->AxisY.AxisAccTime + ifAddOrDec*(-0.02) > 0))
        {
            Pendant_Techn_Param_ptr->AxisY.AxisAcc += ifAddOrDec;
            Pendant_Techn_Param_ptr->AxisY.AxisAccTime += ifAddOrDec*(-0.02);
        }
    }
    if(axisName == "MX") {
        if((Pendant_Techn_Param_ptr->AxisMX.AxisAcc + ifAddOrDec >0) && (Pendant_Techn_Param_ptr->AxisMX.AxisAccTime + ifAddOrDec*(-0.02) > 0))
        {
            Pendant_Techn_Param_ptr->AxisMX.AxisAcc += ifAddOrDec;
            Pendant_Techn_Param_ptr->AxisMX.AxisAccTime += ifAddOrDec*(-0.02);
        }
    }
    if(axisName == "MZ") {
        if((Pendant_Techn_Param_ptr->AxisMZ.AxisAcc + ifAddOrDec >0) && (Pendant_Techn_Param_ptr->AxisMZ.AxisAccTime + ifAddOrDec*(-0.02) > 0))
        {
            Pendant_Techn_Param_ptr->AxisMZ.AxisAcc += ifAddOrDec;
            Pendant_Techn_Param_ptr->AxisMZ.AxisAccTime += ifAddOrDec*(-0.02);
        }
    }
    if(axisName == "SX") {
        if((Pendant_Techn_Param_ptr->AxisSX.AxisAcc + ifAddOrDec >0) && (Pendant_Techn_Param_ptr->AxisSX.AxisAccTime + ifAddOrDec*(-0.02) > 0))
        {
            Pendant_Techn_Param_ptr->AxisSX.AxisAcc += ifAddOrDec;
            Pendant_Techn_Param_ptr->AxisSX.AxisAccTime += ifAddOrDec*(-0.02);
        }
    }
    if(axisName == "SZ") {
        if((Pendant_Techn_Param_ptr->AxisSZ.AxisAcc + ifAddOrDec >0) && (Pendant_Techn_Param_ptr->AxisSZ.AxisAccTime + ifAddOrDec*(-0.02) > 0))
        {
            Pendant_Techn_Param_ptr->AxisSZ.AxisAcc += ifAddOrDec;
            Pendant_Techn_Param_ptr->AxisSZ.AxisAccTime += ifAddOrDec*(-0.02);
        }
    }
    if(axisName == "Ratio") {
        if((Pendant_Techn_Param_ptr->PendantServoParam.SpeedRatio + ifAddOrDec*10 >=0) && (Pendant_Techn_Param_ptr->PendantServoParam.SpeedRatio + ifAddOrDec*10 <=100))
            Pendant_Techn_Param_ptr->PendantServoParam.SpeedRatio += ifAddOrDec*10;
    }
    if(axisName == "Corrective") {
        if(Pendant_Techn_Param_ptr->PendantServoParam.CorrectiveAxisAcc + ifAddOrDec >0)
            Pendant_Techn_Param_ptr->PendantServoParam.CorrectiveAxisAcc += ifAddOrDec;
    }
}

void speed_add_dec::displaySpeedParam()
{
    ui->speed_flexibleLabel1->setText(QString::number(Pendant_Techn_Param_ptr->AxisY.AxisAcc));
    ui->label_axis1_time->setText(QString::number(Pendant_Techn_Param_ptr->AxisY.AxisAccTime));
    ui->speed_flexibleLabel2->setText(QString::number(Pendant_Techn_Param_ptr->AxisMX.AxisAcc));
    ui->label_axis2_time->setText(QString::number(Pendant_Techn_Param_ptr->AxisMX.AxisAccTime));
    ui->speed_flexibleLabel3->setText(QString::number(Pendant_Techn_Param_ptr->AxisMZ.AxisAcc));
    ui->label_axis3_time->setText(QString::number(Pendant_Techn_Param_ptr->AxisMZ.AxisAccTime));
    ui->speed_flexibleLabel4->setText(QString::number(Pendant_Techn_Param_ptr->AxisSX.AxisAcc));
    ui->label_axis4_time->setText(QString::number(Pendant_Techn_Param_ptr->AxisSX.AxisAccTime));
    ui->speed_flexibleLabel5->setText(QString::number(Pendant_Techn_Param_ptr->AxisSZ.AxisAcc));
    ui->label_axis5_time->setText(QString::number(Pendant_Techn_Param_ptr->AxisSZ.AxisAccTime));
    ui->speed_flexibleLabel_1->setText(QString::number(Pendant_Techn_Param_ptr->PendantServoParam.SpeedRatio) + "%");
    ui->speed_flexibleLabel_2->setText(QString::number(Pendant_Techn_Param_ptr->PendantServoParam.CorrectiveAxisAcc));
}

void speed_add_dec::pushReturnMenu()
{
    emit returnMenu();
}

void speed_add_dec::pushReturnTeachMode()
{
    emit returnTeachMode();
}

void speed_add_dec::pushReturnCalTimer()
{
    emit returnCalTimer();
}

speed_add_dec::~speed_add_dec()
{
    delete ui;
}
